package com.sunshine.wrongquestionset.activity.exercise

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textview.MaterialTextView
import com.sunshine.wrongquestionset.R
import com.sunshine.wrongquestionset.callback.OnClickListener

/**
 * @author sunshine
 * @date 2021/4/12
 */
class ExerciseResultRecyclerAdapter(private val callback: OnClickListener) : RecyclerView.Adapter<ExerciseResultRecyclerAdapter.ViewHolder>() {

    private lateinit var context: Context

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val root: LinearLayout = itemView.findViewById(R.id.root_answer)
        val questionPosition: MaterialTextView = itemView.findViewById(R.id.textView_question_position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        context = parent.context
        return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_answer, parent, false))
    }

    override fun getItemCount(): Int {
        return ExerciseActivity.getQuestionList()?.size?:0
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.questionPosition.text = "${position + 1}"
        //不为Null说明这道题答了
        if (ExerciseActivity.getAnswerMap()[position + 1] != null) {
            holder.root.background = ContextCompat.getDrawable(context, R.drawable.ripple_bg_answer_done)
            holder.questionPosition.setTextColor(Color.WHITE)
        } else {
            holder.root.background = ContextCompat.getDrawable(context, R.drawable.ripple_bg_answer_not_done)
        }

        holder.root.setOnClickListener {
            callback.onClick(position + 1)
        }
    }
}